package com.chushouya.product.service.admin.impl;

import com.general.framework.core.lang.*;
import com.chushouya.common.constant.ProductStatusEnum;
import com.chushouya.common.support.Contexts;
import com.general.framework.core.enums.DelFlagEnum;
import com.chushouya.product.dao.entity.ProductEntity;
import com.chushouya.product.dao.repository.ProductRepository;
import com.chushouya.product.dto.admin.product.ProductSaveDTO;
import com.chushouya.product.service.admin.BrandService;
import com.chushouya.product.service.admin.CategoryService;
import com.chushouya.product.service.admin.ProductCreateService;
import com.chushouya.product.service.admin.ProductSeriesService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.math.BigDecimal;

@Service
@Slf4j
public class ProductCreateServiceImpl implements ProductCreateService {

    @Resource
    private CategoryService categoryService;

    @Resource
    private BrandService brandService;

    @Resource
    private ProductSeriesService productSeriesService;

    @Resource
    private ProductRepository productRepository;

    @Override
    public ProductSaveDTO initCreate(Long categoryId, Long brandId, Long seriesId) {
        Check.notNullOrZero(categoryId, "请选择商品分类");
        Check.notNullOrZero(brandId, "请选择商品品牌");

        ProductEntity productEntity = new ProductEntity();
        productEntity.setCategoryId(categoryId);
        productEntity.setCategoryName(categoryService.getCategoryName(categoryId));
        productEntity.setBrandId(brandId);
        productEntity.setBrandName(brandService.getBrandName(brandId));

        if (Longs.notNullAndZero(seriesId)) {
            productEntity.setSeriesId(seriesId);
            productEntity.setSeriesName(productSeriesService.getSeriesName(seriesId));
        } else {
            productEntity.setSeriesId(Numbers.LONG_ZERO);
        }

        productEntity.setMaxPrice(BigDecimal.ZERO);
//        productEntity.setOpenedMaxPrice(BigDecimal.ZERO);
        productEntity.setSort(Numbers.INTEGER_ONE);
        productEntity.setStatus(ProductStatusEnum.cache.value());
        productEntity.setDelFlag(DelFlagEnum.normal.getValue());
        productEntity.setHandleUser(Contexts.getUserContext().getUserName());
        productEntity.setCreateTime(Dates.getTimeNow());
        productEntity.setUpdateTime(Dates.getTimeNow());
        productRepository.insertSelective(productEntity);

        ProductSaveDTO product = new ProductSaveDTO();
        product.setProductId(productEntity.getProductId());
        product.setCategoryId(categoryId);
        product.setCategoryName(productEntity.getCategoryName());
        product.setBrandId(brandId);
        product.setBrandName(productEntity.getBrandName());

        product.setSeriesId(seriesId);
        product.setSeriesName(productEntity.getSeriesName());
//        product.setProductSkuList(Lists.emptyList());
//        product.setProductAttrList(Lists.emptyList());
        return product;
    }

    @Override
    public Long saveProduct(ProductSaveDTO productSaveDTO) {
        Check.notNull(productSaveDTO, "商品保存信息不能为空");
        
        ProductEntity productEntity;
        boolean isUpdate = Longs.notNullAndZero(productSaveDTO.getProductId());
        
        if (isUpdate) {
            // 更新商品
            productEntity = productRepository.selectByPrimaryKey(productSaveDTO.getProductId());
            Check.notNull(productEntity, "商品不存在");
        } else {
            // 新增商品
            productEntity = new ProductEntity();
            productEntity.setCreateTime(Dates.getTimeNow());
            productEntity.setHandleUser(Contexts.getUserContext().getUserName());
            productEntity.setDelFlag(DelFlagEnum.normal.getValue());
            productEntity.setStatus(ProductStatusEnum.cache.value());
        }
        
        // 设置商品基本信息
        if (Strings.isNotEmpty(productSaveDTO.getProductCode())) {
            productEntity.setProductCode(productSaveDTO.getProductCode());
        }
        if (Strings.isNotEmpty(productSaveDTO.getProductName())) {
            productEntity.setProductName(productSaveDTO.getProductName());
        }
        if (Strings.isNotEmpty(productSaveDTO.getDeviceName())) {
            productEntity.setDeviceName(productSaveDTO.getDeviceName());
        }
        if (Strings.isNotEmpty(productSaveDTO.getProductImage())) {
            productEntity.setProductImage(productSaveDTO.getProductImage());
        }
        if (Strings.isNotEmpty(productSaveDTO.getKeywords())) {
            productEntity.setKeywords(productSaveDTO.getKeywords());
        }
        if (productSaveDTO.getMaxPrice() != null) {
            productEntity.setMaxPrice(productSaveDTO.getMaxPrice());
        }
        
        // 设置分类、品牌、系列信息
        if (Longs.notNullAndZero(productSaveDTO.getCategoryId())) {
            productEntity.setCategoryId(productSaveDTO.getCategoryId());
            if (Strings.isNotEmpty(productSaveDTO.getCategoryName())) {
                productEntity.setCategoryName(productSaveDTO.getCategoryName());
            } else {
                productEntity.setCategoryName(categoryService.getCategoryName(productSaveDTO.getCategoryId()));
            }
        }
        
        if (Longs.notNullAndZero(productSaveDTO.getBrandId())) {
            productEntity.setBrandId(productSaveDTO.getBrandId());
            if (Strings.isNotEmpty(productSaveDTO.getBrandName())) {
                productEntity.setBrandName(productSaveDTO.getBrandName());
            } else {
                productEntity.setBrandName(brandService.getBrandName(productSaveDTO.getBrandId()));
            }
        }
        
        if (Longs.notNullAndZero(productSaveDTO.getSeriesId())) {
            productEntity.setSeriesId(productSaveDTO.getSeriesId());
            if (Strings.isNotEmpty(productSaveDTO.getSeriesName())) {
                productEntity.setSeriesName(productSaveDTO.getSeriesName());
            } else {
                productEntity.setSeriesName(productSeriesService.getSeriesName(productSaveDTO.getSeriesId()));
            }
        }
        
        // 设置更新时间
        productEntity.setUpdateTime(Dates.getTimeNow());
        
        if (isUpdate) {
            productRepository.updateByPrimaryKeySelective(productEntity);
            log.info("更新商品成功，商品ID: {}", productEntity.getProductId());
        } else {
            productRepository.insertSelective(productEntity);
            log.info("新增商品成功，商品ID: {}", productEntity.getProductId());
        }
        
        return productEntity.getProductId();
    }
}
